﻿using System.Threading;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System.Collections.Generic;
namespace Chimp.Core
{
    public class UIDoc
    {
        public static Dictionary<string, UIDoc> Docs = new Dictionary<string, UIDoc>();
        public static void RegDoc(string n,UIDoc d)
        {
            Docs.Add(n,d);
        }
        public static void UseDoc(string n)
        {
            var d = Docs[n];
            ChimpUI.UIRoot = d.CreateUI();

        }
        public virtual ChimpWidget CreateUI()
        {
            return null;
        }
    }
    public static class ChimpUI
    {
        public static float MX, MY, MZ;
        public static float XI, YI, ZI;
        public static bool[] BUT = new bool[255];
        public static ChimpWidget Over;
        public static ChimpWidget Active;
        public static ChimpWidget[] Push = new ChimpWidget[255];
        private static Thread UpdateThread;
        private static int TickRate = 35;
        private static readonly Mutex UILock = new Mutex();
        public static ChimpEventHandler Handler = new ChimpEventHandler();
        public static ChimpWidget UIRoot { get; set; }
        public static bool initonce = false;
        public static void InitUI()
        {
            if ( initonce ) return;
            ChimpI.Init();
            UpdateThread = new Thread(THREAD_UpdateUI);
            UpdateThread.Start();
            initonce = true;
        }

        public static void SetRoot(ChimpWidget w)
        {
            UIRoot = w;
        }

        public static void SetPage(string path)
        {
            UILock.WaitOne();

            var page = new JointNode(path);

            page.Root.Debug(1);

            ChimpWidget c = ParseNode(page.Root, null);

            UIRoot = c;

            UILock.ReleaseMutex();
        }

        public static ChimpWidget NewWidget(IFNode n)
        {
            //Control r = null;
            ChimpWidget r = null;
            switch (n.Name)
            {
                case "Frame":

                    break;
                case "Button":
                    var b2 = new UIButton(0, 0, 0, 0, "", null);
                    ParseFrame(n, b2);
                    r = b2;
                    break;
                case "SystemWindow":

                    break;

                    break;
                    //       return b;
            }
            foreach (IFNode n2 in n.Nodes)
            {
                ChimpWidget c = NewWidget(n2);
                r.Widgets.Add(c);
            }
            return r;
        }

        private static void ParseFrame(IFNode n, UIFrame f2)
        {
            ParseWidget(n, f2);
            ParseImage(n, f2);
        }

        private static void ParseImage(IFNode n, UIFrame f2)
        {
            if (n.Has("Image"))
            {
                f2.Image = new ChimpTexture2D(n["Image"]);
            }
        }

        private static void ParseWidget(IFNode n, ChimpWidget w)
        {
        }

        private static ChimpWidget ParseNode(IFNode n, ChimpWidget p)
        {
            ChimpWidget r = NewWidget(n);

            return r;
        }

        public static void SetInput(float x, float y, params bool[] but)
        {
            BUT[0] = but[0];
            BUT[1] = but[1];
            BUT[2] = but[2];
            XI = x - MX;
            YI = y - MY;
            MX = x;
            MY = y;
        }

        public static void THREAD_UpdateUI()
        {
            while (true)
            {
                // 
       //         SetInput(ChimpI.MouseX(), ChimpI.MouseY(), ChimpI.IsMouseDown(MouseButton.Left),
         //                ChimpI.IsMouseDown(MouseButton.Right), ChimpI.IsMouseDown(MouseButton.Middle));


                UILock.WaitOne();
                if (UIRoot != null)
                {
                    UIRoot.UpdateWidget();
                    if (Push[0] == null)
                    {
                        if (UIRoot != null)
                        {
                            UIRoot.UIUpdate();
                        }
                    }
                    if (Push[0] != null)
                    {
                        if (BUT[0] == false)
                        {
                            if (!Push[0].PointInsideWidget(MX, MY))
                            {
                                Push[0].OnExit();
                                Over = null;
                            }
                            Push[0].OnMouseUp(0);
                            Push[0] = null;
                        }
                        else

                        {
                            Push[0].OnMouse(0);
                        }
                        if (Push[0] != null)
                        {
                            Push[0].OnMouseMove(MX, MY, XI, YI);
                            Push[0].OnUpdate(Vector2.Zero);
                        }
                    }
                }
                UILock.ReleaseMutex();

                Thread.Sleep(1);
            }
        }

        public static void RenderUI()
        {
            UILock.WaitOne();
            if (UIRoot != null)
            {
                UIRoot.UIRender();
            }
            UILock.ReleaseMutex();


            GL.Begin(BeginMode.Triangles);

            GL.Color4(0.4f, 1, 1, 0.8f);
            GL.Vertex2(MX, MY);

            GL.Color4(0.2f, 0.6f, 0.6f, 0.4f);
            GL.Vertex2(MX + 20, MY);

            GL.Vertex2(MX, MY + 20);

            GL.End();
        }

        #region Nested type: JobInputUpdate

        public class JobInputUpdate : ChimpJob
        {
            public override void PerformJob()
            {
                while (true)
                {
                    Thread.Sleep(8);
                }
            }
        }

        #endregion
    }
}